<template>
	<view>
		<view class="p-b-20">
			<view class="search" :style="[navStyle]">
				<image src="../../static/icon/search.png" mode=""></image>
				<view class="text">
					<input confirm-type="search" type="text" v-model="keywords" placeholder="搜索关键词"
						@confirm="searchConfirm" />
				</view>
			</view>
		</view>

		<view class="line"></view>

		<view class="content" :style="{'height': contentHeight + 'px'}">
			<view class="left">
				<scroll-view :scroll-y="true" scroll-with-animation :throttle="false"
					:style="{'height': contentHeight + 'px'}" :scroll-into-view="leftScrollViewId">
					<view v-for="(item,index) in categoryList" :key="index"
						:class="{'left-item': index != categoryInex,'left-item-active': index === categoryInex }"
						@click="categoryClick(item,index)" :id="'left-current-' + index">
						<view class="text text-cut">
							{{ item.name }}
						</view>
					</view>
				</scroll-view>
			</view>
			<view class="right">
				<scroll-view @scroll="rightScroll" :scroll-y="true" :scroll-with-animation="true"
					@scrolltolower="scrolltolower" :style="{'height': contentHeight + 'px'}"
					:scroll-into-view="scrollViewId">
					<view class="right-item rightblock" v-for="(item,index) in categoryList" :key="index"
						:id="'current-' + index">
						<view class="title">
							<text>{{ item.name }}</text>
						</view>
						<view class="right-li" v-for="(citem,cindex) in item.childlist" :key="index"
							@click="jumpTo(item.childlist[cindex])">
							<view class="li-left">
								<image :src="citem.ImageFrist" mode=""></image>
							</view>
							<view class="li-right">
								<div class="upper">
									<div class="name text-cut">{{ citem.name }}</div>
								</div>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				contentHeight: 750,
				// categoryList: [{
				// 	id: 0,
				// 	name: '全部'
				// }],
				categoryList: [],
				categoryName: '',
				categoryInex: 0,
				goodsList: [],
				page: 1,
				pageSize: 10,
				last_page: 1,
				keywords: '',
				leftScrollViewId: 0,
				scrollViewId: 0,
				heightArr: [],
			}
		},
		computed: {
			navStyle() {
				// 获取设备系统信息，单位px
				const systemInfo = wx.getSystemInfoSync()
				// 状态栏高度
				const statusBarHeight = systemInfo.statusBarHeight
				// iOS真机 胶囊区高度 40
				let navigationHeight = 50
				// 胶囊区域高度，安卓48
				if (systemInfo.system.indexOf('Android') !== -1) {
					navigationHeight = 58
				}
				return {
					'margin-top': statusBarHeight + navigationHeight + 'rpx'
				}
			},
		},
		onLoad() {
			const systemInfo = wx.getSystemInfoSync()
			this.contentHeight = systemInfo.windowHeight - 100

			this.getCategoryList()
		},
		methods: {
			getCategoryList() {
				let _this = this

				uni.showLoading({
					title: '加载中...',
					mask: true
				})

				_this.$api.get('mall.category/Showlist', {}).then(res => {
					uni.hideLoading()
					if (res.code == 1) {
						let list = _this.categoryList.concat(res.data.categorydata)

						_this.categoryList = list

						if (_this.categoryList.length > 0) {
							// _this.categoryName = _this.categoryList[_this.categoryInex].name
							setTimeout(() => {
								let initArr = [0]
								let initNum = 0
								const query = uni.createSelectorQuery()
								query.selectAll('.rightblock').boundingClientRect()
								query.selectViewport().scrollOffset()
								query.exec(function(res) {
									res[0].map(val => {
										initNum += val.height //实现高度累加
										initArr.push(initNum) //将累加的高度存在数组里
									})

									_this.heightArr = initArr
								})
							}, 300)
						}

						// _this.getGoodsList()
					} else {
						showModal(res.msg)
					}
				}, fail => {})
			},
			categoryClick(item, index) {
				if (index == this.categoryInex) {
					return false
				}

				this.categoryInex = index
				this.scrollViewId = 'current-' + index
				// this.categoryName = this.categoryList[this.categoryInex].name

				// this.keywords = ''
				// this.page = 1
				// this.last_page = 1
				// this.goodsList = []
				// this.getGoodsList()
			},
			rightScroll(e) {
				let _this = this
				let st = e.detail.scrollTop
				let heightArr = _this.heightArr

				for (let i = 0; i < heightArr.length; i++) {
					if ((st > heightArr[i]) && (st < (heightArr[i + 1] - 5))) {
						_this.categoryInex = i
						_this.leftScrollViewId = 'left-current-' + i
					}
				}
			},
			getGoodsList() {
				let _this = this
				_this.$api.get('mall.goods/category_list', {
					page: _this.page,
					page_size: _this.pageSize,
					types: 'normal',
					name: _this.keywords,
					id: _this.categoryList[_this.categoryInex].id
				}).then(res => {
					_this.show = true
					if (res.code == 1) {
						let list = _this.goodsList.concat(res.data.listdata)

						_this.goodsList = list

						_this.last_page = res.data.pagedata.last_page
					} else {
						_this.toast(res.msg)
					}
				}, fail => {})
			},
			jumpTo(item) {
				uni.navigateTo({
					url: '/pages/mall/goods?title=' + item.name + '&category_id=' + item.id
				})
			},
			searchConfirm(e) {
				uni.navigateTo({
					url: '/pages/mall/goods?title=' + e.detail.value + '&keywords=' + e.detail.value
				})
				// this.keywords = e.detail.value
				// this.categoryInex = 0
				// this.page = 1
				// this.last_page = 1
				// this.goodsList = []
				// this.getGoodsList()
			},
			scrolltolower() {

				let _this = this;
				// 已经是最后一页
				if (_this.page >= _this.last_page) {
					return false;
				}

				_this.page++
				_this.getGoodsList()
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #FFFFFF;
	}

	.flex--end {
		display: flex;
		align-items: center;
		justify-content: flex-end;
	}

	.text-cut {
		overflow: hidden;
		-webkit-line-clamp: 1;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		text-overflow: ellipsis;
	}

	.p-b-20 {
		padding-bottom: 20rpx;
	}

	.search {
		background-color: #fff;
		width: 429rpx;
		height: 30px;
		border-radius: 32rpx;
		border: 2rpx solid #DCDCDC;
		display: flex;
		align-items: center;
		justify-content: start;
		padding-left: 28rpx;
		margin-left: 18rpx;

		image {
			width: 32rpx;
			height: 32rpx;
			margin-left: 16rpx;
		}

		.text {
			margin-left: 8rpx;
			font-weight: 400;
			font-size: 24rpx;
			color: #8C8C8C;
		}
	}

	.line {
		height: 10px;
		background-color: #F6F6F6;
	}

	.content {
		width: 100%;
		overflow: hidden;
		display: flex;
		justify-content: space-between;

		.left {
			width: 23%;
			background-color: #F0F0F0;
			overflow: scroll;

			.left-item {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				padding: 26rpx 0 22rpx 0;
			}

			.left-item-active {
				display: flex;
				justify-content: center;
				align-items: center;
				flex-direction: column;
				padding: 26rpx 0 22rpx 0;
				background: #35D14A;
				border-radius: 0rpx 10rpx 10rpx 0rpx;

				.text {
					color: #fff;
					font-weight: 800;
				}

				.text-sub {
					color: #fff;
				}
			}

			image {
				width: 48rpx;
				height: 48rpx;
				margin-bottom: 2rpx;
			}

			.text {
				width: 90%;
				text-align: center;
				font-weight: 400;
				font-size: 28rpx;
				color: #000000;
			}

			.text-sub {
				width: 60%;
				text-align: center;
				font-weight: 400;
				font-size: 16rpx;
				color: #444444;
			}


		}

		.right {
			width: 77%;
			background-color: #FFFFFF;
			padding: 28rpx 24rpx;
			overflow: scroll;
			padding-bottom: 100rpx;

			.right-item {
				padding-bottom: 20rpx;

				.title {
					font-weight: 800;
					font-size: 28rpx;
					color: #000000;
					letter-spacing: 1px;
					margin-bottom: 28rpx;
				}

				.right-li {
					width: 100%;
					overflow: hidden;
					// height: 100rpx;
					margin-bottom: 20rpx;
					display: flex;
					align-items: center;
					justify-content: space-between;
					border-bottom: 2rpx solid #DDDDDD;
					padding-bottom: 20rpx;

					.li-left {
						width: 100rpx;
						height: 100rpx;
						// border-radius: 16rpx;
						overflow: hidden;

						image {
							width: 100%;
							height: 100%;
						}
					}

					.li-right {
						width: calc(100% - 124rpx);
						height: 100rpx;
						// padding-left: 24rpx;
						display: flex;
						align-items: center;
						// flex-direction: column;
						// justify-content: space-between;

						.upper {
							.name {
								font-weight: 800;
								font-size: 28rpx;
								color: #010101;
							}

							.pay {
								font-weight: 400;
								font-size: 20rpx;
								color: #8A8A8A;
								margin-top: 8rpx;
							}
						}

						.lower {
							// display: flex;
							// align-items: center;
							// justify-content: space-between;
							padding-bottom: 10rpx;

							.price {
								font-weight: 800;
								font-size: 32rpx;
								color: #F73830;
							}

							.buy {
								background: #000000;
								border-radius: 40rpx;
								color: #FFFFFF;
								padding: 8rpx 20rpx;
								font-weight: 400;
								font-size: 24rpx;
								color: #FFFFFF;
							}
						}
					}
				}
			}
		}
	}

	.m-l-16 {
		margin-left: 16rpx;
	}
</style>